package com.xuan.collegeblog.spider.service.Impl;

import com.moxi.mogublog.commons.entity.xuan.WeiBoCommentEntity;
import com.moxi.mogublog.commons.entity.xuan.WeiBoTopicEntity;
import com.xuan.collegeblog.spider.service.BaiduService;
import com.xuan.collegeblog.spider.service.ProcessDataService;
import com.xuan.collegeblog.spider.service.WeiBoCommentService;
import com.xuan.collegeblog.spider.service.WeiBoTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ProcessDataServiceImpl implements ProcessDataService {

    private static final String PRAGMA = "下面是一篇微博的标题和评论，" +
            "请根据内容总结出一个较能吸引眼球的一篇微博标题并且精炼内容。" +
            "只用返回你总结的标题和简介和内容，其他的什么都不用说，不用回答我，只用告诉我标题和简介和内容即可，格式如下：" +
            "标题:" +
            "简介:" +
            "内容:";


    @Autowired
    private WeiBoCommentService weiBoCommentService;

    @Autowired
    private WeiBoTopicService weiBoTopicService;

    @Autowired
    private BaiduService baiduService;

    /**
     * @param college_id
     * @return java.lang.String
     * @Description:
     * @Author: huangzx
     * @Date: 2024/7/18 9:46
     */
    @Override
    @Transactional
    public String integratedData(Long college_id) {
        List<WeiBoTopicEntity> topicEntities = weiBoTopicService.getAllByCollegeId(college_id);
        for (WeiBoTopicEntity topicEntity : topicEntities) {
            String mid = topicEntity.getMid();
            try {
                // 删除相关数据
                // weiBoTopicService.deleteByOid(mid);

                // 构建内容
                StringBuilder sb = new StringBuilder();
                sb.append(PRAGMA).append("\n");
                sb.append("微博标题：").append(topicEntity.getTopicContent()).append("\n");
                List<WeiBoCommentEntity> commentEntities = weiBoCommentService.getAllByMid(mid);
                int i = 0;
                sb.append("微博评论：").append("\n");
                for (WeiBoCommentEntity weiBoCommentEntity : commentEntities) {
                    sb.append("评论").
                            append(++i).append("：").
                            append(weiBoCommentEntity.getCommentContent()).append("\n");
                    weiBoCommentService.deleteByUid(weiBoCommentEntity.getUid());
                }

                // 处理内容
                baiduService.processContent(sb.toString(), college_id);
            } catch (Exception e) {
                // 处理异常，例如记录日志
                e.printStackTrace();
                return "操作失败：" + e.getMessage(); // 返回失败消息
            }
        }
        return "操作成功";
    }


}
